<style>
    .clear{
        clear: both;
}
    .hide{
        display: none;
    }
    .width50{
        width: 50px!important;
    }
    .width70p{
        width: 75%;
}
    .width100p{
        width: 100%;
    }
    .width150{
        width:150px;
}
    .left{
        float:left;
}
.margin-R-15{
    margin-right: 15px;
}
.nopad {
    padding: 0;
    text-align: center;
}
.label_simple{
    float:none!important;
    display:inline!important;
    font-weight: normal!important;
}
#publication_information_0{
    border-top: 0px solid #cccccc;
    margin-top: 0px;
    padding-top: 0px;
}
.publication_information_component{
    border-top: 1px solid #cccccc;
    margin-top: 5px;
    padding-top: 5px;
}
</style>
<div class="componentheading"><?php echo $page_title;?></div>
<div><?php global_message(); ?></div>
<?php
//echo "<pre>"; print_r($member_list);exit;
	$isEdit = isset($_borrower) ? true : false;
	$text = empty($isEdit) ? 'Add' : 'Update';
	$selected = 'selected = "selected"';
?>
<form method="post" id="newForm">
<div class="box table">
<table width="100%">
    <tr class="box-header">
        <td colspan="2"><strong>General User Information</strong></td>
    </tr>
    <tr>
    	<td><strong>User Type</strong></td>
        <td>
            <select name="user_type" onchange="changeUserType(this.value);" id="borrower_user_type">
                <option value="non-member">Non-Member</option>
                <option value="member">Member</option>
            </select>
        </td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>First Name</strong></td>
        <td><input type="text" class="required" name="first_name" id="first_name" value="<?php if($isEdit) echo $_borrower->first_name;?>" /></td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>Last Name</strong></td>
        <td><input type="text" class="required" name="last_name" id="last_name" value="<?php if($isEdit) echo $_borrower->last_name;?>" /></td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>Email</strong></td>
        <td><input type="text" class="required email" name="email" id="email" value="<?php if($isEdit) echo $_borrower->email;?>" /></td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>Address1</strong></td>
        <td><input type="text" class="required" name="address1" id="address1" value="<?php if($isEdit) echo $_borrower->address1;?>" /></td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>Address2</strong></td>
        <td><input type="text" class="" name="address2" id="address2" value="<?php if($isEdit) echo $_borrower->address2;?>" /></td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>Phone1</strong></td>
        <td><input type="text" class="required" name="phone1" id="phone1" value="<?php if($isEdit) echo $_borrower->phone1;?>" /></td>
    </tr>
    <tr class="non_member_field">
    	<td><strong>Phone2</strong></td>
        <td><input type="text" class="" name="phone2" id="phone2" value="<?php if($isEdit) echo $_borrower->phone2;?>" /></td>
    </tr>
    <tr class="member_field hide">
    	<td>Member ID</td>
        <td>
            <select name="member_id">
                <?php foreach($member_list as $member_id=>$member_name){?>
                <option value="<?php echo $member_id;?>"><?php echo $member_name;?></option>
                <?php }?>
            </select></td>
    </tr>
    <tr class="box-header">
        <td colspan="2"><strong>Publication Information</strong></td>
    </tr>
    <tr>
    	<td colspan="2" id="publication_information"></td>
    </tr>
    <tr>
    	<td>&nbsp;</td>
        <td>
            <input type="button" onclick="addNewBorrower(cntNo);" value="Add more" />
        </td>
    </tr>



    
    <tr>
    	<td>&nbsp;</td>
        <td>
            <input type="submit" name="submit_borrower" id="submit_borrower" value="<?php echo $text;?>">
            <input type="button" onclick="window.borrower.href = '<?php echo site_url('borrower');?>'" value="Back" />
        </td>
    </tr>
</table>
</div>
</form>
<script>
var cntNo=0;
$(document).ready(function(){
	//$('#newForm').validate();
        defaultVal = $("#borrower_user_type").val();
        changeUserType(defaultVal);
        addNewBorrower(cntNo);

								
});

function addNewBorrower(No){
    var loading_image = "<img src='<?php echo base_url().'/assets/images/fb_loading.gif'?>'>";
    var divID = "publication_information_"+No;
    $("#publication_information").append("<div id='"+divID+"' class='publication_information_component'></div>");
    $("#"+divID).html(loading_image);
    $.ajax({
               type: "POST",
               url: "<?php echo site_url('borrower/ajax_add_borrower_form');?>",
               data: 'cntNo='+No,
               success: function(msg){
                   $("#"+divID).html(msg);
                   changePublicationMax($("#borrower_publication_list_"+No).val(),+No);
               }
           });
      cntNo++;    
}
function changeUserType(user_type){
    if(user_type=="member"){
        $(".non_member_field").addClass('hide');
        $(".member_field").removeClass('hide');
    }else{
        $(".non_member_field").removeClass('hide');
        $(".member_field").addClass('hide');
    }
}
function changePublicationMax(publication_id,ID){
    $.ajax({
               type: "POST",
               url: "<?php echo site_url('borrower/ajax_get_max_publication');?>",
               data: 'publication_id='+publication_id,
               success: function(msg){
                   var msg1=msg.split('~~');
                   var qty_en=msg1[1];
                   var qty_np=msg1[2];
                   $(".borrower_publication_max_en_qty_"+ID).html('(max:'+qty_en+')');
                   $(".borrower_publication_max_np_qty_"+ID).html('(max:'+qty_np+')');
               }
           });
}
</script>
